package cn.edu.pku.houlei;

import java.util.List;

import cn.edu.pku.houlei.HtmlTag.TagType;

public class Test {
	public static void main(String[] args) {
	//	HtmlTree t1 = HtmlTree.create("http://v.youku.com/v_show/id_cd00XMjg4MDUxMTI=.html");
		HtmlTree t1 = HtmlTree.create("http://www.tudou.com/programs/view/vfDDkEV1fWc/");
		visit(t1.getRoot(), 100);
	}
	
	private static void visit(HtmlNode node, int limit)
	{
		visit_inner(node, 0, limit);
	}

	private static void visit_inner(HtmlNode node, int level, int limit)
	{
		if (level > limit)
			return;
		List<HtmlNode> nl = node.getChildren();
		
		if (nl == null)
			return;
		
		int cnt = nl.size();
		
		for (int i = 0; i < cnt; i++)
		{
			HtmlNode n = nl.get(i);
			
			for (int j = 0; j < level; j++)
			{
				System.out.print("\t");
			}
			
			if (n.getTag().tagType != TagType.TAG_PURTEXT && n.getTag().tagType != TagType.TAG_REMARK) {
				System.out.print("<" + n.getTag().getTagName());
				List<HtmlAttribute> attrList = n.getAttributesList();
				int attrCnt = attrList.size();
				for (int j = 0; j < attrCnt; j++)
				{
					HtmlAttribute attr = attrList.get(j);
					System.out.print(" ");
					System.out.print(attr.getAttrName() + "=\"" + attr.getAttrValue() + "\"");
				}
				System.out.println(">");
				
			} else if (n.getTag().tagType == TagType.TAG_REMARK) {
				System.out.println("<!--" + n.getText() + "-->");
			}
			else if (n.getTag().tagType == TagType.TAG_PURTEXT){
				System.out.print("" + n.getText());
			}

			visit_inner(n, level + 1, limit);
			
			System.out.println("\n");
			
			for (int j = 0; j < level; j++)
			{
				System.out.print("\t");
			}
			if (n.getTag().tagType != TagType.TAG_PURTEXT && n.getTag().tagType != TagType.TAG_REMARK) {
				System.out.println("</" + n.getTag().getTagName() + ">");
			} 	
		}
	}
}
